/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * plugin.c
 * Copyright (C) Lucas van Dijk 2011 <info@return1.net>
 * 
 * anjuta-avr-environment is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * anjuta-avr-environment is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <gio/gio.h>
#include <glib/gi18n.h>
#include <libanjuta/anjuta-shell.h>
#include <libanjuta/anjuta-debug.h>
#include <libanjuta/interfaces/ianjuta-document-manager.h>
#include <libanjuta/interfaces/ianjuta-environment.h>

#include "plugin.h"
#include "environment.h"

#define XML_FILE ANJUTA_DATA_DIR"/ui/anjuta_avr_environment.xml"
#define UI_FILE ANJUTA_DATA_DIR"/glade/anjuta_avr_environment.ui"

#define AVR_SECTION "AvrPlugin"
#define TARGET_DEVICE_KEY "TargetDevice"
#define CPU_FREQ_KEY "CpuFrequency"

static gpointer parent_class;

static void
on_session_save (AnjutaShell *shell, AnjutaSessionPhase phase, AnjutaSession *session, AnjutaAVREnvironment *this)
{
	if (phase != ANJUTA_SESSION_PHASE_NORMAL)
	{
		return;
	}

	anjuta_session_set_string(session, AVR_SECTION, TARGET_DEVICE_KEY, this->target_device);
	anjuta_session_set_string(session, AVR_SECTION, CPU_FREQ_KEY, this->cpu_freq);
}

static void 
on_session_load (AnjutaShell *shell, AnjutaSessionPhase phase, AnjutaSession *session, AnjutaAVREnvironment *this)
{
	if (phase != ANJUTA_SESSION_PHASE_NORMAL)
	{
		return;
	}

	g_free(this->target_device);
	g_free(this->cpu_freq);

	this->target_device = anjuta_session_get_string(session, AVR_SECTION, 
	                                                TARGET_DEVICE_KEY);	
	this->cpu_freq = anjuta_session_get_string(session, AVR_SECTION,
	                                           CPU_FREQ_KEY);

	/** Make sure something is allocated */
	if(this->target_device == NULL || this->target_device == '\0')
	{
		g_free(this->target_device);
		this->target_device = g_strdup("");
	}
	
	if(this->cpu_freq == NULL || this->cpu_freq == '\0')
	{
		g_free(this->cpu_freq);
		this->cpu_freq = g_strdup("");
	}
}

static void
on_sample_action_activate (GtkAction *action, AnjutaAVREnvironment *plugin)
{
	GObject *obj;
	IAnjutaEditor *editor;
	IAnjutaDocumentManager *docman;
	
	/* Query for object implementing IAnjutaDocumentManager interface */
	obj = anjuta_shell_get_object (ANJUTA_PLUGIN (plugin)->shell,
									  "IAnjutaDocumentManager", NULL);
	docman = IANJUTA_DOCUMENT_MANAGER (obj);
	editor = IANJUTA_EDITOR (ianjuta_document_manager_get_current_document (docman, NULL));

	/* Do whatever with plugin */
	anjuta_util_dialog_info (GTK_WINDOW (ANJUTA_PLUGIN (plugin)->shell),
							 "Document manager pointer is: '0x%X'\n"
							 "Current Editor pointer is: 0x%X", docman,
							 editor);
}

static GtkActionEntry actions_file[] = {
	{
		"ActionFileSample",                       /* Action name */
		GTK_STOCK_NEW,                            /* Stock icon, if any */
		N_("_Sample action"),                     /* Display label */
		NULL,                                     /* short-cut */
		N_("Sample action"),                      /* Tooltip */
		G_CALLBACK (on_sample_action_activate)    /* action callback */
	}
};

/*
 * Anjuta Plugin Implementation
 * ----------------------------
 */
static gboolean
anjuta_avr_environment_activate (AnjutaPlugin *plugin)
{
	DEBUG_PRINT ("%s", "AnjutaAVREnvironment: Activating AnjutaAVREnvironment plugin ...");
	GError *error;
	AnjutaAVREnvironment *this = (AnjutaAVREnvironment*) plugin;
	AnjutaShell *shell = anjuta_plugin_get_shell(plugin);

	/* Add all UI actions and merge UI */
	/*AnjutaUI *ui = anjuta_shell_get_ui (plugin->shell, NULL);
	
	anjuta_avr_environment->action_group = 
		anjuta_ui_add_action_group_entries (ui, "ActionGroupFileanjuta_avr_environment",
											_("AVR"),
											actions_file,
											G_N_ELEMENTS (actions_file),
											GETTEXT_PACKAGE, TRUE,
											plugin);
	anjuta_avr_environment->uiid = anjuta_ui_merge (ui, XML_FILE);*/

	g_signal_connect(shell, "save-session", G_CALLBACK(on_session_save), this);
	g_signal_connect(shell, "load-session", G_CALLBACK(on_session_load), this);

	this->target_device = NULL;
	this->cpu_freq = NULL;

	return TRUE;
}

static gboolean
anjuta_avr_environment_deactivate (AnjutaPlugin *plugin)
{
	AnjutaAVREnvironment *this = (AnjutaAVREnvironment *) plugin;
	AnjutaUI *ui;

	DEBUG_PRINT ("%s", "AnjutaAVREnvironment: Dectivating AnjutaAVREnvironment plugin ...");

	/*ui = anjuta_shell_get_ui (plugin->shell, NULL);
	anjuta_ui_remove_action_group (ui, ((AnjutaAVREnvironment*)plugin)->action_group);
	anjuta_ui_unmerge (ui, ((AnjutaAVREnvironment*)plugin)->uiid);*/

	g_free(this->target_device);
	g_free(this->cpu_freq);

	this->target_device = NULL;
	this->cpu_freq = NULL;
	
	return TRUE;
}

static void
anjuta_avr_environment_finalize (GObject *obj)
{
	/* Finalization codes here */
	G_OBJECT_CLASS (parent_class)->finalize (obj);
}

static void
anjuta_avr_environment_dispose (GObject *obj)
{
	/* Disposition codes */
	G_OBJECT_CLASS (parent_class)->dispose (obj);
}

static void
anjuta_avr_environment_instance_init (GObject *obj)
{
	AnjutaAVREnvironment *plugin = (AnjutaAVREnvironment*) obj;

	plugin->uiid = 0;

	plugin->widget = NULL;
	plugin->target_device = NULL;
	plugin->cpu_freq = NULL;

}

static void
anjuta_avr_environment_class_init (GObjectClass *klass) 
{
	AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);

	parent_class = g_type_class_peek_parent (klass);

	plugin_class->activate = anjuta_avr_environment_activate;
	plugin_class->deactivate = anjuta_avr_environment_deactivate;
	klass->finalize = anjuta_avr_environment_finalize;
	klass->dispose = anjuta_avr_environment_dispose;
}

/*
 * IAnjutaEnvironment interface implementation
 * -------------------------------------------
 */

static gboolean anjuta_environment_override (IAnjutaEnvironment *obj,
    gchar **dirp, gchar ***argvp, gchar ***envp, GError **err)
{
	/**
	 * TODO: Add options for target device etc to the configure flags
	 */
	return TRUE;
}

static gchar*
anjuta_environment_get_real_dir(IAnjutaEnvironment* obj, gchar* dir, GError** err)
{
	return dir;
}

static GtkWidget*
anjuta_environment_get_extra_widget (IAnjutaEnvironment* obj, GError** err)
{
	AnjutaAVREnvironment *plugin = (AnjutaAVREnvironment *) obj;

	GtkWidget *widget = load_configure_widget(plugin);

	return widget;
}

static void 
ienvironment_iface_init(IAnjutaEnvironmentIface *iface)
{
	iface->override = anjuta_environment_override;
	iface->get_real_directory = anjuta_environment_get_real_dir;
	iface->get_extra_configure_widget = anjuta_environment_get_extra_widget;
}

ANJUTA_PLUGIN_BEGIN (AnjutaAVREnvironment, anjuta_avr_environment);
ANJUTA_PLUGIN_ADD_INTERFACE(ienvironment, IANJUTA_TYPE_ENVIRONMENT);
ANJUTA_PLUGIN_END;

ANJUTA_SIMPLE_PLUGIN (AnjutaAVREnvironment, anjuta_avr_environment);
